#!/bin/bash

#To get the list of HSC users from /etc/passwd file
#The UserMObject.java will call this script

if [ "$1" == "" ]
then
  echo Usage: getUserList option
  echo        option    -list to list all the HSC users
  exit 1
fi

if [ "$1" == "-list" ]
then
USER=""
for i in hmc
do

   #to get the group id for HSC Groups
   GROUP_ID=`grep $i /etc/group | cut -f 3 -d:`
   #echo $GROUP_ID
   #to get the user list for the given group id
   GROUP_ID_SEARCH=$GROUP_ID
   if [ "$GROUP_ID_SEARCH" == "" ]
   then
      continue
   fi

   USER_NAME=`grep $GROUP_ID_SEARCH /etc/passwd | cut -f 1 -d:`
   #echo user$USER_NAME
   for m in $USER_NAME
   do
        if [ "$m" == "" ]
	then
	   continue
	fi
	
   	GROUP_NAME=`/usr/bin/id -gn $m`
   	#echo groupname$GROUP_NAME
	if [ "$GROUP_NAME" == "$i" ] 
       	then
            USER_COUNT=`grep -c -F -w $m:x: /etc/passwd`
            if [ "$USER_COUNT" == "1" ]
            then
              USER_ID_USER=`grep -F -w $m:x: /etc/passwd | cut -f 3 -d:`
            fi

            if [ "$USER_COUNT" != "1" ]
            then

              USER_USER=`grep -F -w $m:x /etc/passwd | cut -f 3 -d:`

              for d in $USER_USER
              do

                USER_ID_NAME=`grep -F -w $m:x:$d /etc/passwd | cut -f 1 -d:`
                if [ "$USER_ID_NAME" == "$m" ]
                then
                    USER_ID_USER=$d
                    break;
                fi
              done
             fi

	     #USER_ID_USER=`grep -w $m:x /etc/passwd | cut -f 3 -d:`
      	     GROUP_USER=`grep -F $m:x:$USER_ID_USER /etc/passwd | cut -f 1,2,3,5 -d:`
    	     USER="$USER$GROUP_USER,"
         fi 
   done 	
done
ROOT_USER="root:x:0:root,"
USER="$USER$ROOT_USER"
echo $USER
fi


if [ "$1" == "-properties" ]
then
	if [ "$2" != "" ]
        then
           echo `grep :x:$2: /etc/passwd | cut -f 1,3,5 -d:`	  
        fi
fi

if [ "$1" == "-Role" ]
then
     	if [ "$2" != "" ]
        then
           echo `cat /opt/hsc/data/RoleRegistration.db | cut -f 1,2 -d:`        
        fi

fi

if [ "$1" == "-userroles" ]
then
    USER=`grep :x:$2: /etc/passwd | cut -f 1 -d:`
    GROUP=`grep -F -w $USER /etc/group |cut -f 1 -d:`
    DESCRIPTION=`cat /opt/hsc/data/RoleRegistration.db | cut -f 2 -d:`
    #DESCRIPTION="System Administrator,Service Representative,Advanced Operator,Operator,User Administrator"
    PROPERTIES=`grep :x:$2: /etc/passwd | cut -f 1,3,5 -d:`
    echo "$GROUP~$DESCRIPTION~$PROPERTIES"

fi

if [ "$1" == "-userdelete" ]
then
    USER=`grep :x:$2: /etc/passwd | cut -f 1 -d:`
    GROUP=`grep -F $USER /etc/group |cut -f 1 -d:`
    echo $GROUP
fi
